package com.oracle.malldemo.servlet.users;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oracle.malldemo.domain.Users;
import com.oracle.malldemo.view.JsonData;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet(value = "/users/reg")
public class UsersRegServlet extends UsersServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        try {

            //获得json字符串，json字符串-》json对象-》Java对象
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (ServletInputStream) req.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb);

            //字符串转换json对象。
            //JSONObject jsonObject = JSONUtil.parseObj(sb.toString());
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            Users regUser = JSON.parseObject(jsonObject.toJSONString(), Users.class);
            System.out.println("要注册的用户对象是：");
            System.out.println(regUser);

            //判断一下用户输入的验证码是否正确。
            String yoursRandomCode = regUser.getRandomCode();

            if (req.getSession().getAttribute("sessionRandomCode") == null) {
                JsonData result = JsonData.fail("验证码验证失败！");
                out.write(JSONObject.toJSONString(result));
                out.flush();
                out.close();
                return;
            }

            String sessionRandomCode = (String) req.getSession().getAttribute("sessionRandomCode");
            if (!yoursRandomCode.equals(sessionRandomCode)) {
                JsonData result = JsonData.fail("验证码验证失败！");
                out.write(JSONObject.toJSONString(result));
                out.flush();
                out.close();
                return;
            }

            if (usersService.reg(regUser)) {
                JsonData result = JsonData.success("用户注册成功！");
                out.write(JSONObject.toJSONString(result));
            } else {
                JsonData result = JsonData.fail("用户注册失败！");
                out.write(JSONObject.toJSONString(result));
            }
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            JsonData result = JsonData.fail("程序异常！");
            out.write(JSONObject.toJSONString(result));
            out.flush();
            out.close();
        }
    }
}
